jQuery.noConflict();
(function($) {
    $.page=function(){
        var $page={};
        $page.methods={
            init:function(){
                $page.slider.init();
                $page.zoom.init();
				//选颜色
				$(".pdpColorImg a").click(function(){

					$(this).addClass("cur").siblings().removeClass("cur");
				});
				//发送电子邮件
				$(".pdpEmailTo").click(function(){
				    window.open($(this).attr("href"),'newwindow','height=500, width=720, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no');
					return false;
				});
				//监听cms上传图片后异步导致的问题
				//$page.cmsImages.load();    
				//筛选
			    $('.filterVal').click(function(e){
			    	$('.filterBox ul').slideToggle(200);
			    	e.stopPropagation();
			    });
			    $('.filterBox ul li').click(function(e){
			    	var starHtml = $(this).html();
			    	$('.filterVal').empty();
			    	$('.filterVal').append(starHtml);
			    });
				$(document).click(function(){
					$('.filterBox ul').hide();
				});	
				$('.popSize').click(function(){
					$('.popSize').hide();
				});	
				$('.popSizeContent').click(function(event){
		            event.stopPropagation();
		        });
			    $('.pdpSizeTxt').click(function(e){
			    	$('.popSize').show();
			    	e.stopPropagation();
			    });
            },
            addCartComplete:function(data,el){
                $common.productStruct.replayStyle();
				$page.methods.askdataAddCart($common.dev.getAjaxUrl("miniShoppingcar"));
            },
			askdataAddCart:function(obj){
				 new $.setAjax({url:obj.url,data:obj.data,type:obj.type,dataType:"json",
                	success:function(data){$page.methods.updataAddCart(data);}
           		});
			},
			updataAddCart:function(data){
				/*console.log(data.info.length);
				if(data.info.length == 0){
					isDate = false;
					$(".manicarBox").hide();
					if(isOver)$(".emptyCart").show();
				}else{
					isDate = true;
					$(".emptyCart").hide();
					$(".minicart .minicartNum .miniNum").html(data.qty);
					$(".minicart").addClass("bgMiniCart");
				}
				$common.loading.hide();*/
				$(".minicart").removeClass("hover");
				$(".minicart .minicartNum .miniNum").html(data.qty);
			},
            countDown:function(data,obj,status){
                if(obj.key=="hardlaunch"){
                    $(".time.btnBlueYellowLinear").html("距离开售还有："+data.day+"天 "+ data.hours+"时 "+ data.minute+"分 "+ data.second+"秒");
                }
                if(obj.key=="presale"){
                    $(".time.btnGrayLinear").html("距离预售开始还有："+data.day+"天 "+ data.hours+"时 "+ data.minute+"分 "+ data.second+"秒");
                }
                if(obj.key=="presaleEnd"){
                    $(obj.dom).html(data.day+"天 "+ data.hours+"时 "+ data.minute+"分 "+ data.second+"秒");
                }
            }
        };
        $page.cmsImages={
            load:function(){
                var count=0,total=$(".pdpTxt img").length;
                $(".pdpTxt img").each(function(index,item){
                    if (item.complete) {count++; $page.cmsImages.loadComplete(count,total); return true;}
                    item.onload = function () {count++;$page.cmsImages.loadComplete(count,total);return true;};
                    item.error = function () {count++;$page.cmsImages.loadComplete(count,total);return true;};
              });
            },
           loadComplete:function(count,total){
               if(count==total){$(".pdpTxt").height($(".pdpTxt").outerHeight());}
           }
        };
        $page.slider={
            init:function(){
                //放大镜缩略图slider
                new touchslider('.pdpSmallImgSlids', {
                	nextButton: '.pdpSmallImgSlids-Next',
                	prevButton: '.pdpSmallImgSlids-Prev',
                	size:6,
                	scrollSize:6,
                	direction:'vertical',
                    moveFinishEvent:function(crtPage,totalPage){
                    	var items = $('.pdpSmallImgSlids').find("a").length;
                    	if(items<7){
                    		$('.pdpSmallImgSlids-Next,.pdpSmallImgSlids-Prev').hide();
                    	}
		                $('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Next").removeClass("unable");
		                $('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Prev").removeClass("unable");
		                if(crtPage==1){$('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Prev").addClass("unable");}
		                if(crtPage==totalPage){$('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Next").addClass("unable");}
                    }
                });
                //你可能也会喜欢slider
                new touchslider('.pdpSuggestSlides', {
                	nextButton: '.nextH',
                	prevButton: '.prevH',
                	size:4,
                	scrollSize:4,
                	direction:'horizontal',
                	namespace:'touchslider',
					iconContent:$('.pagination'),
					iconbtnHtml:"",
                    moveFinishEvent:function(crtPage,totalPage){
                        $page.slider.arrowStyle(".pdpSuggestList",crtPage,totalPage);
                    }
                 });
            },
            //更新箭头样式
            arrowStyle:function(el,crtPage,totalPage){
                $(el).find(".nextH").removeClass("unable");
                $(el).find(".prevH").removeClass("unable");
                if(crtPage==1){$(el).find(".prevH").addClass("unable");}
                if(crtPage==totalPage){$(el).find(".nextH").addClass("unable");}
            }
        };
        //放大镜功能
        $page.zoom = {
            //操作事件
            event:function(){
                $(".skuBox").click(function(e)
                {
                    // $(this).mouseover(function(e){
                    //     // 显示的小图片  
                    //     var x = e.pageX

                    //     console.log(x);
                    // });
                   


                    $page.zoom.zoomImgLoad();
                });//点击加载
                $(".pdpSmallImgSlids a").click(function(){$page.zoom.updataBigImage($(this)); return false;});//更新大图
            },
            //缩略图点击， 更新大图
            updataBigImage:function(el){
                this.isPlay=false;
                this.zoomImgLoadComplete(true);
                el.addClass("cur").siblings().removeClass("cur");
                $(".skuBox .skuZoom")[0].src=el.find("img").attr("data-hero");
            },
            //开始加载大图片
            zoomImgLoad:function(){
                if(this.loading)return false;
                this.loading=true;
                $('.skuImgLoading').show();
                var img=new Image();
                img.src=$(".pdpSmallImgSlids .cur img").attr("data-super");
                if(img.complete){$page.zoom.zoomImgLoadComplete();return false;}
                img.onload = function () {$page.zoom.zoomImgLoadComplete();return false;};
            },
            //大图加载完成
            zoomImgLoadComplete:function(isPlay){
                $('.skuBox .skuZoomSuper').attr("src",$(".pdpSmallImgSlids .cur img").attr("data-super"));
                $('.skuImgLoading').hide();
                this.loading=false;
                if(isPlay){this.isPlay=false;}else{this.isPlay=!this.isPlay;this.status();}
                this.zoom.methods.updata(this.isPlay);
            },
            //更新+号icon的状态
            status:function(status){
                if(!this.isPlay){$('.skuOri').show();}else{$('.skuOri').hide();}
            },
            //放大镜初始数据入口
            init:function(){
                  this.isPlay=false;
                  this.loading=false;
                  this.event();
                  this.zoom =  $.zoom(".skuBox",{isZoom:false,
                      hoverEvent:function(type){
                         if(type=="over"){if(!$page.zoom.isPlay){$('.skuOri').show();}else{$('.skuOri').hide();}}else{$('.skuOri').hide();}
                      },
                      //mouseover事件， 传递相关参数。处理+号icon的运动轨迹
                      moveEvent:function(obj){        
                          var xpos=obj.pageX-$(".skuOri").width()/2-obj.spacingLeft;
                          xpos=xpos<0?0:xpos;
                          xpos=xpos>obj.smallWidth-$(".skuOri").width()?obj.smallWidth-$(".skuOri").width():xpos;
                          var ypos=obj.pageY-$(".skuOri").height()/2-obj.spacingTop;
                          ypos=ypos<0?0:ypos;
                          ypos=ypos>obj.smallHeight-$(".skuOri").height()?obj.smallHeight-$(".skuOri").height():ypos;
                          
                          $('.skuZoom').mouseover(function(){
                            // console.log(obj.pageX);
                          });

                          $('.skuOri').css({"left":xpos,"top":ypos});
                      }
                });
				
            }
        };
        return $page.methods;
    };

})(jQuery);

